package com.et114.modules.order.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.modules.cust.BIZ;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.myet.service.IMyEt_UCenterService;
import com.et114.modules.myet.service.MyEt_UCenterServiceImp;
import com.et114.modules.myet.vo.MyEtOrder_VO;
import com.et114.modules.order.form.OrderForm;
import com.et114.modules.order.service.IMng_OrderService;
import com.et114.modules.order.service.Mng_OrderServiceImp;
import com.et114.modules.order.vo.OrderTrack;
import com.et114.modules.order.vo.ProVO;

/**
 * 订单信息查询
 * @author zhiweip
 * @version 1.0
 * @created 09-十一月-2010 23:03:48
 */
public class Mng_OrderModifyAction  extends BaseDispatchAction {
	 
	private IMng_OrderService mng_OrderService = ( Mng_OrderServiceImp )getBean ( "mng_OrderService" ) ;
	private IMng_CustService mng_CustService	= ( Mng_CustServiceImp ) getBean ( "mng_CustService" );
	private IMyEt_UCenterService	m_IMyEt_UCenterService	= ( MyEt_UCenterServiceImp ) getBean ( "m_IMyEt_UCenterService" );
	private String					COUNTRY_ZH;
	@ Override
	public ActionForward unspecified ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ;
		
		// TODO Auto-generated method stub
		return super.unspecified ( mapping , form , request , response );
	}


	//Mng_CustForm
	public ActionForward modify ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		 
		OrderForm cform=(OrderForm)form;
		COUNTRY_ZH="zh";
		
		OrderTrack vo=cform.getOrd ( ); 
		
		MyEtOrder_VO et=new MyEtOrder_VO();
		et.setItemid ( vo.getItemid ( ) );
		et.setItemkind ( vo.getItemkind ( ) );
		et.setItemkindID ( vo.getItemkindID ( ) );
		et.setSupid ( vo.getSupid ( ) );
		et.setSerpid ( vo.getSerpid ( ) );
		et.setItemtype ( vo.getItemtype ( ) );
		try{
			MyEtOrder_VO ppvo=getDetail(et);
			if(ppvo!=null){
				vo.setItemimg ( ppvo.getItemimg ( ) );
				vo.setSerplan ( ppvo.getSerplan ( ) );
			} 
		}catch(Exception e){
			logger.error ( e.getMessage ( ) , e ) ; 
		}
		Integer orderid=mng_OrderService.updateOrder ( vo ,request);
		
		String opflag=request.getParameter ( "opflag" );
		
		if(opflag!=null&&opflag.equalsIgnoreCase ( "reInput" )){
			request.setAttribute ( "back" ,
					"Mng_OrderQueryAction.go?method=inputOrder&orderid=" + orderid );
		}else if(opflag!=null&&opflag.equalsIgnoreCase ( "redo" ))
			request.setAttribute ( "back" , "Mng_OrderQueryAction.go?method=chooseTypes&custid="+vo.getCustid ( ));
		else
			request.setAttribute ( "back" , "Mng_OrderQueryAction.go");
		
		if(opflag!=null&&opflag.equalsIgnoreCase ( "fromModifyOrder" )){
			request.setAttribute ( "back" , "Mng_OrderModifyAction.go?method=toModify&orderid="+orderid );
		}
		return mapping.findForward ( "success" ) ; 
	}
	
	private MyEtOrder_VO getDetail ( MyEtOrder_VO et) throws Exception {
		if(et==null) return null;
			et.setCountry_ZH ( COUNTRY_ZH );
			String itemtype = et.getItemtype ( );
			ProVO detail = new ProVO ( );
			if ( itemtype.equals ( BIZ.TYPES_CERT ) ) {
				detail = m_IMyEt_UCenterService.getDetailCard ( et , null );
				
			} else if ( itemtype.equals ( BIZ.TYPES_PC ) ) {
				detail = m_IMyEt_UCenterService.getDetailPc ( et , null );
				
			} else if ( itemtype.equals ( BIZ.TYPES_SAFE ) ) {
				detail = m_IMyEt_UCenterService.getDetailSafe ( et , null );
				
			} else if ( itemtype.equals ( BIZ.TYPES_TEL ) ) {
				detail = m_IMyEt_UCenterService.getDetailTel ( et , null );
				
			} else if ( itemtype.equals ( BIZ.TYPES_TV ) ) {
				detail = m_IMyEt_UCenterService.getDetailTV ( et , null );
				
			}
			if(detail==null) return null;
			PropertyUtils.copyProperties ( et , detail );
		return et;
	}
	
	public ActionForward toModify ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		
		OrderForm cform = ( OrderForm ) form;
		OrderTrack vo = cform.getOrd ( );
		String lang="zh";
		vo.setLanguage ( lang );
		String orderid=request.getParameter ( "orderid" );
		if(orderid!=null)
			vo.setOrderid ( new Integer(orderid) );
		
		Object o=mng_OrderService.viewOrder ( vo); 
		Object info=mng_OrderService.loadOrderInfo ( vo );
		
		request.setAttribute ( "ORDERDETAIL" , mng_OrderService.loadOrderDetail ( vo ));; 
		request.setAttribute ( "OrderBase" , o );
		request.setAttribute ( "OrderInfo" , info );
		
		if(o!=null){
			OrderTrack InfoVO=(OrderTrack)o;
			CustBaseVo cust=new CustBaseVo();
			cust.setCustid ( InfoVO.getCustid ( ) );
			request.setAttribute ( "BASEINFO" , mng_CustService .loadCust ( cust ) );
		}
		String comeFrom=request.getRequestURL ( ).toString ( );
		request.setAttribute ( "comeFrom" , comeFrom);
		
		String q=request.getParameter ( "url" );
		//System.out.println ("=====url:==ff========="+q);
		if(q!=null){
			//System.out.println ("url:"+q);
			q=q.replaceAll ( "#","?" );
			//System.out.println ("url:"+q);
			q=q.replaceAll ( "____","=" );
			//System.out.println ("url:"+q);
			q=q.replaceAll ( "-","&amp;" );
			//System.out.println ("=====url:============="+q);
			request.setAttribute ( "URL" , q );
		}
		return mapping.findForward ( "toModify" );
	}
		
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delBag ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		
		OrderForm cform = ( OrderForm ) form;
		OrderTrack vo = cform.getOrd ( );
		String lang="zh";
		vo.setLanguage ( lang );
		String orderid=request.getParameter ( "orderid" );
		if(orderid!=null)
			vo.setOrderid ( new Integer(orderid) );
		
		OrderTrack po=(OrderTrack)mng_OrderService.loadOrder ( vo); 
		if(!po.getIsaudi ( ).equals ( "0" )){
			request.setAttribute ( "msg" , "订单已审核,不能添加!" );
			request.setAttribute ( "back" , "Mng_OrderQueryAction.go?method=orderQuery" );
			return mapping.findForward ( "info" ) ;
		}
		
		String did = request.getParameter ( "did" ); 
		vo.setDetailid ( new Integer(did) );
		mng_OrderService.deleteOrderDetail ( vo );
		vo.setDetailid ( null );
		
		Object o=mng_OrderService.viewOrder ( vo); 
		Object info=mng_OrderService.loadOrderInfo ( vo );
		
		request.setAttribute ( "ORDERDETAIL" , mng_OrderService.loadOrderDetail ( vo ));; 
		request.setAttribute ( "OrderBase" , o );
		request.setAttribute ( "OrderInfo" , info );
		
		if(o!=null){
			OrderTrack InfoVO=(OrderTrack)o;
			CustBaseVo cust=new CustBaseVo();
			cust.setCustid ( InfoVO.getCustid ( ) );
			request.setAttribute ( "BASEINFO" , mng_CustService .loadCust ( cust ) );
		}
		return mapping.findForward ( "toModify" );
	}
	
	public ActionForward toUpdate ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		OrderForm cform = ( OrderForm ) form;
		OrderTrack vo = cform.getOrd ( );
		String orderid=request.getParameter ( "orderid" );
		vo.setOrderid ( new Integer(orderid) );
		String lang="zh";
		vo.setLanguage ( lang );
		
		Object o=mng_OrderService.viewOrder ( vo); 
		Object info=mng_OrderService.loadOrderInfo ( vo );
		
		request.setAttribute ( "ORDERDETAIL" , mng_OrderService.loadOrderDetail ( vo ));; 
		request.setAttribute ( "OrderBase" , o );
		request.setAttribute ( "OrderInfo" , info );
		
		if(o!=null){
			OrderTrack InfoVO=(OrderTrack)o;
			CustBaseVo cust=new CustBaseVo();
			cust.setCustid ( InfoVO.getCustid ( ) );
			request.setAttribute ( "BASEINFO" , mng_CustService .loadCust ( cust ) );
		}
		String comeFrom=request.getParameter ( "comeFrom" );
		if(comeFrom!=null)
		request.setAttribute ( "comeFrom" , comeFrom);
		
		return mapping.findForward ( "toModify" );
	}

}